package controllers

import (
	"NewEdu/models"
	"NewEdu/utils"
	"strconv"
	"time"

	beego "github.com/beego/beego/v2/server/web"
)

// 问答管理
type TAnswerController struct {
	beego.Controller
}

// GetAll ...
// @Title QueryUserAnswer
// @Description QueryUserAnswer
// @Param	Authorization	header	string	true	" Token"
// @Param	sn		path 	string	true		"学号"
// @Param	withUser		path 	string	true		"是否需要用户信息，填1 是需要，填0是不需要"
// @Success 200 {object} models.TAnswer
// @Failure 403
// @router /queryUserAnswerList/:sn/:withUser [get]
func (c *TAnswerController) QueryUserAnswerList() {
	snStr := c.Ctx.Input.Param(":sn")
	list, err := models.QueryUserAnswerList(snStr)
	if err != nil {
		c.Data["json"] = utils.Export(1001, "QueryUserAnswerList 不对", err.Error())
		c.ServeJSON()
		return
	}
	result := make(map[string]interface{})
	withUserStr := c.Ctx.Input.Param(":withUser")
	if withUserStr == "1" {
		var user *models.TUser
		user, err = models.GetTUserById(snStr)
		if err != nil {
			return
		}
		var depart *models.TDepart
		depart, err = models.GetTDepartById(user.TDepartId)
		if err != nil {
			return
		}
		user.Class = depart.Label
		result["hornor"], err = models.GetTAchievmentBySn(snStr)
		if err != nil {
			return
		}
		result["user"] = user
	}
	result["answer"] = list
	c.Data["json"] = utils.Export(1000, "ok", result)
	c.ServeJSON()
}

// GetUserAnswer ...
// @Title GetUserAnswer
// @Description GetUserAnswer
// @Param	Authorization	header	string	true	" Token"
// @Param	id		path 	int	true		"回答的answer的id"
// @Success 200 {object} models.TAnswer
// @Failure 403
// @router /queryUserAnswer/:id [get]
func (c *TAnswerController) GetUserAnswer() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	item, err := models.QueryUserAnswerItem(id)
	if err != nil {
		c.Data["json"] = utils.Export(1001, "QueryUserAnswerItem 不对", err.Error())
		c.ServeJSON()
		return
	}
	c.Data["json"] = utils.Export(1000, "ok", item)
	c.ServeJSON()
}

// SaveUserAnswer ...
// @Title SaveUserAnswer
// @Description SaveUserAnswer
// @Param	Authorization	header	string	true	" Token"
// @Param	sn		path 	string	true		"学号"
// @Param	templateid		query 	int	true		"模板id"
// @Param	kind		query 	int	false		"访谈的类型，0是一般访谈，1是留学访谈，"
// @Param	body		body 	string	true		"对话记录"
// @Success 200 {object} models.TAnswer
// @Failure 403
// @router /saveUserAnswer/:sn [post]
func (c *TAnswerController) SaveUserAnswer() {
	kind, _ := c.GetInt8("kind", 0)
	templateid, _ := c.GetInt("templateid", 0)
	if templateid == 0 {
		c.Data["json"] = utils.Export(1001, "templateid error", "")
		c.ServeJSON()
		return
	}
	currentTemplate, err := models.GetTemplateByID(templateid)
	if err != nil {
		c.Data["json"] = utils.Export(1001, "GetTemplateByID error", err)
		c.ServeJSON()
		return
	}
	snStr := c.Ctx.Input.Param(":sn")
	currentUser := c.Ctx.Input.GetData(models.CurrentUser).(*utils.UserInfo)

	var answer models.TAnswer
	answer.AnswerSN = snStr
	answer.AskSN = currentUser.SN
	answer.AskName = currentUser.Name
	answer.Creattime = time.Now()
	answer.QuestionTemplate = currentTemplate.Label
	answer.Kind = kind
	answer.Chat = string(c.Ctx.Input.RequestBody)

	answer.Analysis, err = models.CallDeepSeek(answer.Chat, currentTemplate.AnalysisPrompt, answer.Kind)
	if err != nil {
		c.Data["json"] = utils.Export(1001, "CallDeepSeek error", err)
		c.ServeJSON()
		return
	}

	err = models.AddTAnswer(&answer)
	if err != nil {
		c.Data["json"] = utils.Export(1001, "AddTAnswer error", err)
		c.ServeJSON()
		return
	}
	c.Data["json"] = utils.Export(1000, "ok", answer.Analysis)
	c.ServeJSON()
}
